package org.openbpm.base.autoconfiguration;


import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * WebMvcConfigurers base class
 */
public class BaseWebMvcConfigurers
{
    //自定义字符串转换器
    @Bean
    public StringHttpMessageConverter stringHttpMessageConverter(){
        StringHttpMessageConverter converter  = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        return converter;
    }

    //自定义fastjson转换器
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters(){
        //1.需要定义一个convert转换消息的对象;
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();

        //2:添加fastJson的配置信息;
        // 配置序列化策略 long/bigint/bigDecimal字段转换为string
        SerializerFeature[] serializerList = {SerializerFeature.BrowserCompatible,SerializerFeature.WriteDateUseDateFormat,SerializerFeature.WriteBigDecimalAsPlain};

        //3处理中文乱码问题
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.TEXT_HTML);
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);

        //4.在convert中添加配置信息.
        fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
        fastJsonHttpMessageConverter.setFeatures(serializerList);

//        FastJsonConfig fjc = new FastJsonConfig();
//        fjc.setSerializerFeatures(SerializerFeature.BrowserCompatible,SerializerFeature.WriteDateUseDateFormat,SerializerFeature.WriteBigDecimalAsPlain);
//        SerializeConfig serializeConfig = SerializeConfig.globalInstance;
//        serializeConfig.put(BigInteger.class, com.alibaba.fastjson.serializer.ToStringSerializer.instance);
//        serializeConfig.put(BigDecimal.class, com.alibaba.fastjson.serializer.ToStringSerializer.instance);
//        serializeConfig.put(Long.class, com.alibaba.fastjson.serializer.ToStringSerializer.instance);
//        serializeConfig.put(Long.TYPE, com.alibaba.fastjson.serializer.ToStringSerializer.instance);
//        fjc.setSerializeConfig(serializeConfig);
//        fastJsonHttpMessageConverter.setFastJsonConfig(fjc);

        HttpMessageConverter<?> converter = fastJsonHttpMessageConverter;

        return new HttpMessageConverters(converter);

    }
}
